package top.yeyusmile.service;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yeyusmile.pojo.*;
import top.yeyusmile.utils.HttpUtil;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author 夜雨
 * @Web www.yeyusmile.top
 * @date 2022/3/16 15:15
 */
@Slf4j
@Service
public class ConvenientServiceImpl implements ConvenientService {

    @Autowired
    private SendMsgService sendMsgService;

    //private MyQQMessage myQQMessage;


    @Override
    public void historyToday(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "https://api.heroa.cn:3403/convenience/history_today/";
        HttpUtil.sendHttps(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // System.out.println(response.body().string());
                HistoryTody historyTody = new Gson().fromJson(response.body().string(), HistoryTody.class);
                sendMsgService.sendMsg(historyTody.toString(), message);
            }
        });
        /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }

    @Override
    public synchronized void SixtyReadWorld(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "https://api.heroa.cn:3403/convenience/sixty_second_read_world/";
        HttpUtil.sendHttps(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // System.out.println(response.body().string());
                SixtyReadWorld sixtyReadWorld = new Gson().fromJson(response.body().string(), SixtyReadWorld.class);
                sendMsgService.sendMsg(sixtyReadWorld.toString(), message);
            }
        });
    }


    @Override
    public void diary(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "https://api.heroa.cn:3403/random/diary/";
        HttpUtil.sendHttps(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // System.out.println(response.body().string());
                CommonResult commonResult = new Gson().fromJson(response.body().string(), CommonResult.class);
                sendMsgService.sendMsg(commonResult.toString(), message);
            }
        });
    }

    @Override
    public synchronized void quotation(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "https://api.heroa.cn:3403/random/quotation/";
        HttpUtil.sendHttps(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // System.out.println(response.body().string());
                CommonResult commonResult = new Gson().fromJson(response.body().string(), CommonResult.class);
                sendMsgService.sendMsg(commonResult.toString(), message);

            }
        });
    }

    @Override
    public void lookGirl(MyQQMessage message) {

        //this.myQQMessage = message;
        int st = new Random(System.currentTimeMillis()).nextInt(99) * 10;
        String url = "https://pic.sogou.com/napi/pc/searchList?mode=1&start=" + st + "&xml_len=48&query=%E7%BE%8E%E5%A5%B3";
        HttpUtil.sendHttps(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String resp = response.body().string();

                JsonParser jsonParser = new JsonParser();
                JsonElement element = jsonParser.parse(resp);
                JsonObject root = element.getAsJsonObject();
                int index = new Random(System.currentTimeMillis()).nextInt(47);
                //data.items[0].picUrl
                String picUrl = root
                        .getAsJsonObject("data").
                        getAsJsonArray("items")
                        .get(index).getAsJsonObject()
                        .get("picUrl").getAsString();
                Map<String, Object> map = sendMsgService.uploadPic(picUrl, message);
                sendMsgService.sendMsg((String) ((Map) map.get("data")).get("ret"), message);
            }
        });
    }

    @Override
    public void qqLucky(String qq, MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "http://qxu66.xyz/api/qq_xj.php?qq=" + qq;
        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();
                QQLucky qqLucky = new Gson().fromJson(retStr, QQLucky.class);
                sendMsgService.sendMsg(qqLucky.getData().toString(), message);
            }
        });
    }

    @Override
    public void raokouling(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "http://qxu66.xyz/api/rkl.php";
        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();
                sendMsgService.sendMsg(retStr, message);
            }
        });
    }

    @Override
    public void joke(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "http://111.229.174.142:12345/query=&type=xiaohua";
        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();
                sendMsgService.sendMsg(retStr, message);
            }
        });
    }

    @Override
    public void duanzi(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "http://111.229.174.142:12345/query=&type=duanzi";
        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();
                sendMsgService.sendMsg(retStr, message);
            }
        });
    }

    @Override
    public void weather(String weather, MyQQMessage message) {
        //this.myQQMessage = message;
        String url = null;
        try {
            url = "http://111.229.174.142:12345/query=" + URLEncoder.encode(weather, "utf-8") + "&type=weather1";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string().replaceAll("var dataSK=", "");
                if (!"失败".equals(retStr)) {
                    Weather w = new Gson().fromJson(retStr, Weather.class);
                    sendMsgService.sendMsg(w.toString(), message);
                }
            }
        });
    }

    @Override
    public void lookAnime(MyQQMessage message) {
        //this.myQQMessage = message;
        String url = "https://api.mtyqx.cn/api/random.php";
        HttpUtil.sendHttps(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


                log.info(e.toString());
            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {

                //     String picUrl = response.header("location");
                //   String picUrl = response.body().string();
                String picUrl = response.request().url().toString();
                Map<String, Object> map = sendMsgService.uploadPic(picUrl, message);
                sendMsgService.sendMsg((String) ((Map) map.get("data")).get("ret"), message);
            }
        });
    }
}
